//
//  LZYTabBar.m
//  LZYTabbarController
//
//  Created by 刘宇 on 2018/5/3.
//  Copyright © 2018年 瀚宇悟道. All rights reserved.
//

#import "LZYTabBar.h"
#import "LZYTabBarButton.h"
#import "UIView+LZY.h"
#import "CAAnimation+LZYAnimation.h"

@interface LZYTabBar ()
/* 存放 LZYTabBarButton 数组 */
@property (nonatomic, strong) NSMutableArray *saveTabBarArrM;
/* norImageArrM */
@property (nonatomic, strong) NSMutableArray *norImageArrM;
/* selImageArrM */
@property (nonatomic, strong) NSMutableArray *selImageArrM;
/* titleArrM */
@property (nonatomic, strong) NSMutableArray *titleArrM;
@end

@implementation LZYTabBar

- (NSMutableArray *)saveTabBarArrM{
    if (!_saveTabBarArrM) {
        _saveTabBarArrM = [NSMutableArray array];
    }
    return _saveTabBarArrM;
}

- (NSMutableArray *)norImageArrM{
    if (!_norImageArrM) {
        _norImageArrM = [NSMutableArray array];
    }
    return _norImageArrM;
}

- (NSMutableArray *)selImageArrM{
    if (!_selImageArrM) {
        _selImageArrM = [NSMutableArray array];
    }
    return _selImageArrM;
}

- (NSMutableArray *)titleArrM{
    if (!_titleArrM) {
        _titleArrM = [NSMutableArray array];
    }
    return _titleArrM;
}

- (instancetype)initWithFrame:(CGRect)frame norImageArr:(NSArray *)norImageArr SelImageArr:(NSArray *)selImageArr TitleArr:(NSArray *)titleArray config:(LZYTabBarConfig *)tabBarConfig{
    if (self = [super initWithFrame:frame]) {
        for (int i = 0; i < norImageArr.count; i++) {
            LZYTabBarButton *lzytabBarButton = [[LZYTabBarButton alloc]init];
            lzytabBarButton.imageView.image = [UIImage imageNamed:norImageArr[i]];
            lzytabBarButton.title.text = titleArray[i];
            lzytabBarButton.title.textColor = [[LZYTabBarConfig shareConfig] norTitleColor];
            lzytabBarButton.typeLayout = tabBarConfig.typeLayout;
            lzytabBarButton.tag = i;
            [self addSubview:lzytabBarButton];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
            [lzytabBarButton addGestureRecognizer:tap];
            
            [self.saveTabBarArrM addObject:lzytabBarButton];
            self.titleArrM = [NSMutableArray arrayWithArray:titleArray];
            self.norImageArrM = [NSMutableArray arrayWithArray:norImageArr];
            self.selImageArrM = [NSMutableArray arrayWithArray:selImageArr];
        }
    
        //背景颜色处理
        self.backgroundColor = [[LZYTabBarConfig shareConfig] tabBarBackground];
    
        //顶部线条处理
        if (tabBarConfig.isClearTabBarTopLine) {
            [self topLineIsClearColor:YES];
        } else {
            [self topLineIsClearColor:NO];
        }
    
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    NSMutableArray *tempArr = [NSMutableArray array];
    for (UIView *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton removeFromSuperview];
        }
        if ([tabBarButton isKindOfClass:[LZYTabBarButton class]] || [tabBarButton isKindOfClass:[UIButton class]]) {
            [tempArr addObject:tabBarButton];
        }
    }
    
    //进行排序
    for (int i = 0; i < tempArr.count; i++) {
        UIView *view = tempArr[i];
        if ([view isKindOfClass:[UIButton class]]) {
            [tempArr insertObject:view atIndex:view.tag];
            [tempArr removeLastObject];
            break;
        }
    }
    
    CGFloat viewW = self.width / tempArr.count;
    CGFloat viewH = 49;
    CGFloat viewY = 0;
    
    for (int i = 0; i < tempArr.count; i++) {
        CGFloat viewX = i * viewW;
        UIView *view = tempArr[i];
        if (i == 2 && tempArr.count == 5) {
            viewY = -20;
        }else{
            viewY = 0;
        }
        view.frame = CGRectMake(viewX, viewY, viewW, viewH);
    }
}

- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self setUpSelectedIndex:tap.view.tag];
    
    if ([self.lzyTabBarDelegate respondsToSelector:@selector(tabBar:didSelectIndex:)]) {
        [self.lzyTabBarDelegate tabBar:self didSelectIndex:tap.view.tag];
    }
}

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    
    [self setUpSelectedIndex:selectedIndex];
}

#pragma mark - 设置选中的index进行操作
- (void)setUpSelectedIndex:(NSInteger)selectedIndex {
    for (int i = 0; i < self.saveTabBarArrM.count; i++) {
        LZYTabBarButton *tbBtn = self.saveTabBarArrM[i];
        if (i == selectedIndex) {
            tbBtn.title.textColor = [[LZYTabBarConfig shareConfig] selTitleColor];
            tbBtn.imageView.image = [UIImage imageNamed:self.selImageArrM[i]];
            
            LZYConfigTabBarAnimType type = [[LZYTabBarConfig shareConfig] tabBarAnimType];
            if (type == LZYConfigTabBarAnimTypeRotationY) {
                [tbBtn.imageView.layer addAnimation:[CAAnimation LZY_TabBarRotationY] forKey:@"rotateAnimation"];
            } else if (type == LZYConfigTabBarAnimTypeScale) {
                
                CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
                CGPoint point = tbBtn.imageView.frame.origin;
                point.y -= 15;
                anim.toValue = [NSNumber numberWithFloat:point.y];
                
                CABasicAnimation *anim1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
                anim1.toValue = [NSNumber numberWithFloat:1.3f];
                
                CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
                groupAnimation.fillMode = kCAFillModeForwards;
                groupAnimation.removedOnCompletion = NO;
                groupAnimation.animations = [NSArray arrayWithObjects:anim,anim1, nil];
                
                [tbBtn.imageView.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
            } else if (type == LZYConfigTabBarAnimTypeBoundsMin) {
                [tbBtn.imageView.layer addAnimation:[CAAnimation LZY_TabBarBoundsMin] forKey:@"min"];
            } else if (type == LZYConfigTabBarAnimTypeBoundsMax) {
                [tbBtn.imageView.layer addAnimation:[CAAnimation LZY_TabBarBoundsMax] forKey:@"max"];
            }
        } else {
            tbBtn.title.textColor = [[LZYTabBarConfig shareConfig] norTitleColor];
            tbBtn.imageView.image = [UIImage imageNamed:self.norImageArrM[i]];
            [tbBtn.imageView.layer removeAllAnimations];
        }
    }
}

#pragma mark - 顶部线条处理(清除颜色)
- (void)topLineIsClearColor:(BOOL)isClearColor {
    UIColor *color = [UIColor clearColor];
    if (!isClearColor) {
        color = [[LZYTabBarConfig shareConfig] tabBarTopLineColor];
    }
    
    CGRect rect = CGRectMake(0, 0, self.width, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self setBackgroundImage:[UIImage new]];
    [self setShadowImage:img];
}

/**
 *  @author 刘占雨, 18-05-07 10:45:00
 *
 *  让超出父视图的子视图响应事件
 */
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        for (UIView *subView in self.subviews) {
            CGPoint tp = [subView convertPoint:point fromView:self];
            if (CGRectContainsPoint(subView.bounds, tp)) {
                view = subView;
            }
        }
    }
    return view;
}
    

@end
